package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.Subscription;
import com.elinkway.infinitemovies.bean.SubscriptionAlbum;
import com.elinkway.infinitemovies.bean.SubscriptionMainData;
import com.elinkway.infinitemovies.bean.SubscriptionVideo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaopeipei on 2016/9/7.
 */
public class SubscriptionParser extends LVideoMobileParser<Subscription> {

    private static final String NAME = "name";

    private static final String DESC = "desc";

    private static final String SOURCE = "source";

    private static final String URL = "url";

    private static final String ICON = "icon";

    private static final String ALBUM_LIST = "album_list";

    private static final String AID = "aid";

    private static final String EPISODES = "episodes";

    private static final String NOW_EPISODE = "now_episode";

    private static final String VIDEO_LIST = "video_list";

    private static final String PORDER = "porder";

    private static final String DURATION = "duration";

    private static final String VIDEO_TYPE = "video_type";

    private static final String IMAGE = "image";

    private static final String SRC = "src";

    private static final String SUB_SRC = "sub_src";

    private JSONArray mArr;

    @Override
    public Subscription parse(JSONObject data) throws Exception {
        Subscription subscription = new Subscription();
        List<SubscriptionMainData> subscriptionMainDataList = new ArrayList();
        for (int s = 0; s < mArr.length(); s++) {
            SubscriptionMainData subscriptionMainData = new SubscriptionMainData();
            subscriptionMainData.setName(mArr.getJSONObject(s).optString(NAME));
            subscriptionMainData.setSource(mArr.getJSONObject(s).optString(SOURCE));
            subscriptionMainData.setDesc(mArr.getJSONObject(s).optString(DESC));
            subscriptionMainData.setIcon(mArr.getJSONObject(s).optString(ICON));
            subscriptionMainData.setUrl(mArr.getJSONObject(s).optString(URL));
            if (mArr.getJSONObject(s).has(ALBUM_LIST)) {
                JSONArray arr = mArr.getJSONObject(s).optJSONArray(ALBUM_LIST);
                List<SubscriptionAlbum> subscriptionAlbumList = new ArrayList<>();
                for (int i = 0; i < arr.length(); i++) {
                    SubscriptionAlbum subscriptionAlbum = new SubscriptionAlbum();
                    JSONObject obj = arr.getJSONObject(i);
                    subscriptionAlbum.setName(obj.optString(NAME));
                    subscriptionAlbum.setAid(obj.optString(AID));
                    subscriptionAlbum.setEpisodes(Integer.parseInt(obj.optString(EPISODES)));
                    subscriptionAlbum.setNowEpisode(obj.optString(NOW_EPISODE));
                    subscriptionAlbum.setSub_src(obj.optString(SUB_SRC));
                    subscriptionAlbum.setSrc(obj.optString(SRC));
                    JSONArray array = obj.getJSONArray(VIDEO_LIST);
                    List<SubscriptionVideo> subscriptionVideoList = new ArrayList<>();
                    for (int j = 0; j < array.length(); j++) {
                        SubscriptionVideo subscriptionVideo = new SubscriptionVideo();
                        JSONObject object = array.getJSONObject(j);
                        subscriptionVideo.setName(object.optString(NAME));
                        subscriptionVideo.setUrl(object.optString(URL));
                        subscriptionVideo.setPorder(object.optString(PORDER));
                        subscriptionVideo.setEpisodes(object.optString(EPISODES));
                        subscriptionVideo.setDuration(object.optString(DURATION));
                        subscriptionVideo.setImage(object.optString(IMAGE));
                        subscriptionVideo.setVideoType(object.optString(VIDEO_TYPE));
                        subscriptionVideoList.add(subscriptionVideo);
                    }
                    subscriptionAlbum.setVideoList(subscriptionVideoList);
                    subscriptionAlbumList.add(subscriptionAlbum);
                }
                subscriptionMainData.setAlbumList(subscriptionAlbumList);
            }
            subscriptionMainDataList.add(subscriptionMainData);
        }
        subscription.setSubscription(subscriptionMainDataList);
        return subscription;
    }

    @Override
    protected JSONObject getData(String data) throws JSONException {
        // TODO Auto-generated method stub
        mArr = new JSONArray(data);
        data = "{\"data\":\"data\"}";
        return super.getData(data);
    }
}
